package uk.co.hs.service.admin;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import uk.co.hs.dao.web.admin.WebUrlDao;
import uk.co.hs.domain.web.WebUrl;
import uk.co.hs.exceptions.DataAccessExceptions;
import uk.co.hs.service.admin.interfaces.WebUrlServiceInterface;
import uk.co.hs.web.util.AdminListDTO;

@Service("hs_webUrlService")
@Transactional(value="hs_transactionManager")
public class WebUrlService implements WebUrlServiceInterface
{
  @Resource
  private WebUrlDao mWebUrlDao;

  /**
   * @see uk.co.hs.service.admin.interfaces.WebUrlServiceInterface
   * #getAll()
   * {@inheritDoc}
   */
  public List<WebUrl> getAll()
  {
    return mWebUrlDao.getAll();
  }

  /**
   * @see uk.co.hs.service.admin.interfaces.WebUrlServiceInterface
   * #createWebUrl(uk.co.hs.domain.web.WebUrl)
   * {@inheritDoc}
   */
  public void createWebUrl(WebUrl aWebUrl)
  {
    mWebUrlDao.makePersistent(aWebUrl);
  }

  /**
   * @see uk.co.hs.service.admin.interfaces.WebUrlServiceInterface
   * #deleteWebUrl(uk.co.hs.domain.web.WebUrl)
   * {@inheritDoc}
   */
  public void deleteWebUrl(WebUrl aWebUrl)
  {
    mWebUrlDao.makeTransient(aWebUrl);
  }

  /**
   * @see uk.co.hs.service.admin.interfaces.WebUrlServiceInterface
   * #getAllSortedRecords()
   * {@inheritDoc}
   */
  public List<AdminListDTO> getAllSortedRecords()
  {
    List<AdminListDTO> listItems = new ArrayList<AdminListDTO>();
    List<Object> currentObjectList;
    WebUrl currentUrl;
    List<WebUrl> urls = mWebUrlDao.getAll();

    List<Object> currentList;
    int userCount = urls.size();
    for (int index = 0; index < userCount; index++)
    {
      currentUrl = urls.get(index);
      currentObjectList = new ArrayList<Object>();
      currentObjectList.add(currentUrl.getDescription());
      currentObjectList.add(currentUrl.getUrl());

      currentList = new ArrayList<Object>();
      currentList.add(currentUrl.getPk());

      listItems.add(new AdminListDTO(currentList, currentObjectList));
    }
    return listItems;
  }

  /**
   * @see uk.co.hs.service.admin.interfaces.WebUrlServiceInterface
   * #saveAll(java.util.List)
   * {@inheritDoc}
   */
  public String saveAll(List<WebUrl> aUrlsToSave)
  {
    try
    {
      mWebUrlDao.batchMakePersistent(aUrlsToSave);
    }
    catch (DataIntegrityViolationException aException)
    {
      return DataAccessExceptions.DATA_INTEGRITY_VIOLATION;
    }
    catch (HibernateOptimisticLockingFailureException aException)
    {
      return DataAccessExceptions.ALREADY_UPDATED;
    }
    catch (DataAccessException aException)
    {
      return DataAccessExceptions.DATA_ACCESS_FAILURE;
    }
    return null;
  }

  @Override
  public WebUrl getByUrl(String aUrl)
  {
    return mWebUrlDao.getByUrl(aUrl);
  }

  @Override
  public List<WebUrl> getAllSortedByUrl()
  {
    return mWebUrlDao.getAllSortedByUrl();
  }
}
